package com.htu.tax.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.htu.tax.entity.Company;
import com.htu.tax.mapper.CompanyMapper;
import com.htu.tax.payload.base.PageRequest;
import com.htu.tax.payload.request.company.AddCompanyRequest;
import com.htu.tax.payload.request.company.GetCompanyRequest;
import com.htu.tax.payload.request.company.UpdateCompanyRequest;
import com.htu.tax.payload.request.company.UpdateCompanyStateRequest;
import com.htu.tax.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {
    @Override
    public void addCompany(AddCompanyRequest request) {
        Company company = new Company();
        Snowflake companyId = cn.hutool.core.util.IdUtil.createSnowflake(3, 1);
        company.setId(companyId.nextId())
                .setCompanyName(request.getCompanyName())
                .setContactName(request.getContactName())
                .setContactPhone(request.getContactPhone())
                .setCreateTime(LocalDateTime.now())
                .setState(1);
        this.save(company);

    }

    @Override
    public IPage<Company> getCompany(GetCompanyRequest request) {
        return baseMapper.getCompany(PageRequest.getPage(request), request);
    }

    @Override
    public void updateCompany(UpdateCompanyRequest request) {
        Company company = this.getById(Long.valueOf(request.getId()));
        BeanUtils.copyProperties(request, company);
        company.setUpdateTime(LocalDateTime.now());
        this.updateById(company);
    }

    @Override
    public void updateCompanyState(UpdateCompanyStateRequest request) {
        Company company = this.getById(Long.valueOf(request.getId()));
        company.setState(2);
        this.updateById(company);
    }
}
